def solve(nums,k):
    import heapq
    from collections import Counter
    minhp=[]
    counts=dict(Counter(nums))
    for num,freq in counts.items():
        if len(minhp)<k: heapq.heappush(minhp,(freq,num))
        else:
            if freq>minhp[0][0]:heapq.heappushpop(minhp,(freq,num))
    return [i[1] for i in minhp[::-1]]

nums = [1,1,1,2,2,3]
k = 2
print(solve(nums,k))
print(solve([1],1))